﻿#region Copyright and License Notice
// Copyright (C)2010-2012 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>.
#endregion
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using ConfigGen.Utilities;
using ConfigGen.Utilities.Extensions.System.Reflection;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.EndToEndTests
{
    [TestFixture]
    public class CsvEndToEndTests
    {
        [Test]
        public void CsvFileCanBeUsed()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                const string setting1Value = "Setting1_Value";
                const string setting2Value = "Setting2_Value";
                const string setting3Value = "Setting3_Value";

                const string csvFormat =
@"MachineName,ConfigFilePath,Setting1,Setting2,Setting3

DevServer,App.Config,{0},{1},{2}
";

                string csvFileContents = string.Format(csvFormat, setting1Value, setting2Value, setting3Value);

                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.csv");
                File.WriteAllText(settingsFile, csvFileContents);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var doc = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config"));

                var setting = doc.Descendants("Setting1").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting1: element should be present");
                Assert.AreEqual(setting1Value, setting.Value, "Setting1: incorrect value");

                setting = doc.Descendants("Setting2").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting2: element should be present");
                Assert.AreEqual(setting2Value, setting.Value, "Setting2: incorrect value");

                setting = doc.Descendants("Setting3").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting3: element should be present");
                Assert.AreEqual(setting3Value, setting.Value, "Setting3: incorrect value");
            }
        }

        [Test]
        public void CsvFile_With_blank_lines_reads_to_end()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                const string setting1Value = "Setting1_Value";
                const string setting2Value = "Setting2_Value";
                const string setting3Value = "Setting3_Value";

                const string csvFormat =
@"MachineName,ConfigFilePath,Setting1,Setting2,Setting3

,,,,
,,,,


DevServer,App.Config,{0},{1},{2}

,,,,
";

                string csvFileContents = string.Format(csvFormat, setting1Value, setting2Value, setting3Value);

                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.csv");
                File.WriteAllText(settingsFile, csvFileContents);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var doc = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config"));

                var setting = doc.Descendants("Setting1").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting1: element should be present");
                Assert.AreEqual(setting1Value, setting.Value, "Setting1: incorrect value");

                setting = doc.Descendants("Setting2").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting2: element should be present");
                Assert.AreEqual(setting2Value, setting.Value, "Setting2: incorrect value");

                setting = doc.Descendants("Setting3").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting3: element should be present");
                Assert.AreEqual(setting3Value, setting.Value, "Setting3: incorrect value");
            }
        }

        [Test]
        public void CsvFile_with_commas_in_a_token_value()
        {
            using (var testDirectory = new DisposableDirectory())
            {
                const string setting1Value = "Setting1_Value";
                const string setting2Value = "This,Is,Setting,2";
                const string setting3Value = "Setting3_Value";

                const string csvFormat =
@"MachineName,ConfigFilePath,Setting1,Setting2,Setting3

DevServer,App.Config,{0},""{1}"",{2}
";

                string csvFileContents = string.Format(csvFormat, setting1Value, setting2Value, setting3Value);

                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.csv");
                File.WriteAllText(settingsFile, csvFileContents);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var doc = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config"));

                var setting = doc.Descendants("Setting1").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting1: element should be present");
                Assert.AreEqual(setting1Value, setting.Value, "Setting1: incorrect value");

                setting = doc.Descendants("Setting2").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting2: element should be present");
                Assert.AreEqual(setting2Value, setting.Value, "Setting2: incorrect value");

                setting = doc.Descendants("Setting3").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting3: element should be present");
                Assert.AreEqual(setting3Value, setting.Value, "Setting3: incorrect value");
            }
        }

        [Test]
        public void CsvFile_with_a_newline_in_a_token_value()
        {
           using (var testDirectory = new DisposableDirectory())
            {
                const string setting1Value = "Setting1_Value";
                const string setting2Value = @"This Is 
Setting2";
                const string setting3Value = "Setting3_Value";

                const string csvFormat =
@"MachineName,ConfigFilePath,Setting1,Setting2,Setting3

DevServer,App.Config,{0},""{1}"",{2}
";

                string csvFileContents = string.Format(csvFormat, setting1Value, setting2Value, setting3Value);

                string settingsFile = Path.Combine(testDirectory.FullName, "App.Config.Settings.csv");
                File.WriteAllText(settingsFile, csvFileContents);

                string templateFile = Path.Combine(testDirectory.FullName, "App.Config.Template.xml");
                Assembly.GetExecutingAssembly().CopyEmbeddedResourceFileTo("TestResources.App.Config.Template.xml", templateFile);

                var settings = new Preferences
                {
                    SettingsFile = settingsFile,
                    TemplateFile = templateFile,
                    OutputDirectory = Path.Combine(testDirectory.FullName, "Configs"),
                };

                var results = TestHelper.ExecuteCfgTest(settings);

                TestHelper.CheckExitResults(results, false);

                var doc = XElement.Load(Path.Combine(testDirectory.FullName, "Configs\\DevServer\\App.Config"));

                var setting = doc.Descendants("Setting1").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting1: element should be present");
                Assert.AreEqual(setting1Value, setting.Value, "Setting1: incorrect value");

                setting = doc.Descendants("Setting2").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting2: element should be present");
                string newlineNormalizedSetting2Value = Regex.Replace(setting.Value, @"\r\n|\n\r|\n|\r", "\r\n");
                Assert.AreEqual(setting2Value, newlineNormalizedSetting2Value, "Setting2: incorrect value");

                setting = doc.Descendants("Setting3").FirstOrDefault();
                Assert.IsNotNull(setting, "Setting3: element should be present");
                Assert.AreEqual(setting3Value, setting.Value, "Setting3: incorrect value");
            }
        }
    }
}
